package com.haruhi.bot.endpoint.ws.client;

import javax.websocket.CloseReason;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.Session;

public class ClientEndpointAdapter extends Endpoint {

    private WebSocketHandler handler;

    public ClientEndpointAdapter(WebSocketHandler handler){
        this.handler = handler;
    }

    @Override
    public void onOpen(Session session, EndpointConfig endpointConfig) {
        ClientEndpointAdapter self = this;
        session.addMessageHandler(new MessageHandler.Whole<String>() {
            @Override
            public void onMessage(String s) {
                self.handler.onMessage(session,s);
            }
        });
        self.handler.onOpen(session,endpointConfig);
    }

    @Override
    public void onClose(Session session, CloseReason closeReason) {
        this.handler.onClose(session, closeReason);
    }

    @Override
    public void onError(Session session, Throwable throwable) {
        this.handler.onError(session, throwable);
    }
}
